package com.wu.springbootmqconsumer.receive;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.wu.springbootmqconsumer.beans.Product;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.io.IOException;


@Service
//@RabbitListener(queues = {"queue1","queue2"})
public class ReceiveMsgService {


   //  @RabbitListener(queues = "queue10")
    //监听死信队列
    @RabbitListener(queues = "xj_dead_queue")
    public void receiveMsg(String msg, Channel channel, Message message) throws IOException {
        Product product = null;
        try {
            product = new ObjectMapper().readValue(msg, Product.class);

          //  System.out.println("product========="+product);
            //业务，假设业务出现了异常，这个消息消费了但是呢 实际并没有消费到，所以需要手动确认



            //参数1 该条消息的索引
            //参数2 是否批量
            //手动ACK
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
             //拒绝
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            e.printStackTrace();
        }
        System.out.println("接收MSG："+product);
    }

}
